#coding:utf-8
from django.shortcuts import render_to_response
from django.http import *

def show_color(request):
    if "favorite_color" in request.COOKIES:
        return HttpResponse("Your favorite color is %s" % request.COOKIES["favorite_color"])
    else:
        return HttpResponse("You don't have a favorite color.")

def set_color(request):
    if "favorite_color" in request.GET:
        response = HttpResponse("Your favorite color is now %s" % request.GET["favorite_color"])
        response.set_cookie("favorite_color", request.GET["favorite_color"])
        return response
    else:
        return HttpResponse("You didn't give a favorite color.")

def post_comment(request):
    if 'comment' not in request.GET:
        raise Http404('Comment not submitted')

    if request.session.get('has_commented', False):
        return HttpResponse("You've already commented.")

    comment = request.GET['comment']
    print comment

    request.session['has_commented'] = True

    return HttpResponse('Thanks for your comment!')